Skip to content

preserving the capacity of the Symbols slice during resets#7404

Merged
friedrichg merged 4 commits intocortexproject:masterfrom
SungJin1212:preserve-capacity-symbols-in-resets
Apr 25, 2026
Merged

preserving the capacity of the Symbols slice during resets#7404
friedrichg merged 4 commits intocortexproject:masterfrom
SungJin1212:preserve-capacity-symbols-in-resets

Conversation

@SungJin1212
Copy link
Copy Markdown
Member

This PR optimizes memory allocations in the PRW2 path.

  • Added a Reset() method to the PreallocWriteRequestV2 object that clears the fields while preserving the underlying memory capacity of the Symbols slice.
  • Updated the push handler to reduce memory allocations by reusing request objects from a pool PreallocWriteRequestV2FromPool().

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@SungJin1212 SungJin1212 marked this pull request as draft April 9, 2026 01:36
Comment thread pkg/util/push/push.go
// v1 request is put back into the pool by the Distributor.
defer func() {
cortexpb.ReuseWriteRequestV2(&req)
req.Free()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete the req.Free() since this path is a http.

Comment thread pkg/util/push/push.go
}

var req cortexpb.PreallocWriteRequestV2
req := cortexpb.PreallocWriteRequestV2FromPool()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a req from the pool to reuse the capacity of the Symbols.

Comment on lines +133 to +140
// Reset implements proto.Message and preserves the capacity of the Symbols slice.
func (p *PreallocWriteRequestV2) Reset() {
savedSymbols := p.Symbols
p.WriteRequestV2.Reset()
p.Symbols = savedSymbols[:0]
p.data = nil
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Override a Reset() to preserve the capacity of the Symbols.

@SungJin1212 SungJin1212 force-pushed the preserve-capacity-symbols-in-resets branch from c4cc846 to 402e968 Compare April 17, 2026 07:11
@SungJin1212 SungJin1212 marked this pull request as ready for review April 17, 2026 07:42
@SungJin1212 SungJin1212 force-pushed the preserve-capacity-symbols-in-resets branch 2 times, most recently from ab91623 to e8fa471 Compare April 21, 2026 05:13
Copy link
Copy Markdown
Member

@friedrichg friedrichg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Apr 25, 2026
@friedrichg
Copy link
Copy Markdown
Member

I ran some quick benchmarks and a saw only 5% improvement in latency and 10% less memory.
@SungJin1212 can you add some benchmarks so we can confirm the improvements?

Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
@SungJin1212 SungJin1212 force-pushed the preserve-capacity-symbols-in-resets branch from c09ea3e to c581065 Compare April 25, 2026 02:29
@pull-request-size pull-request-size Bot added size/L and removed size/M labels Apr 25, 2026
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
@SungJin1212 SungJin1212 force-pushed the preserve-capacity-symbols-in-resets branch from c581065 to 6054dee Compare April 25, 2026 02:30
@SungJin1212
Copy link
Copy Markdown
Member Author

SungJin1212 commented Apr 25, 2026

@friedrichg
These are the benchmark results.
The memory saving effect seems minimal. However, it will reduce GC pressure.

goos: darwin
goarch: amd64
pkg: github.com/cortexproject/cortex/pkg/util/push
cpu: VirtualApple @ 2.50GHz
Benchmark_HandlePRW2_PoolVsScratch
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=32
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=32-14         	   11965	     99255 ns/op	  183221 B/op	     847 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=32
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=32-14      	   12240	     97542 ns/op	  184356 B/op	     854 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=128
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=128-14        	    2773	    435044 ns/op	  309323 B/op	     945 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=128
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=128-14     	    2805	    432313 ns/op	  313912 B/op	     954 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=512
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=512-14        	     724	   1634150 ns/op	  891172 B/op	    1332 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=512
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=512-14     	     747	   1653845 ns/op	  910150 B/op	    1344 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=2048
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=2048-14       	     144	   8398614 ns/op	 3170082 B/op	    2879 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=2048
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=2048-14    	     141	   8374090 ns/op	 3270083 B/op	    2891 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=4096
Benchmark_HandlePRW2_PoolVsScratch/pool/symbols=4096-14       	      63	  18059987 ns/op	 7222898 B/op	    4938 allocs/op
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=4096
Benchmark_HandlePRW2_PoolVsScratch/scratch/symbols=4096-14    	      68	  18319238 ns/op	 7459255 B/op	    4947 allocs/op
PASS

Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
friedrichg

This comment was marked as low quality.

@friedrichg friedrichg merged commit 551b1f4 into cortexproject:master Apr 25, 2026
64 of 65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size/L type/performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants